package com.sk.bilibili.utils;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;

import com.sk.bilibili.bean.Discuss;
import com.sk.bilibili.bean.FollowThem;
import com.sk.bilibili.bean.Member;

import java.util.List;

public class StringUtils {

    public static SpannableStringBuilder getColorText(String text,int color,int...pos){
        SpannableStringBuilder builder=new SpannableStringBuilder(text);
        for (int i = 0; i < pos.length; i+=2) {
            ForegroundColorSpan span = new ForegroundColorSpan(color);
            builder.setSpan(span,pos[i],pos[i+1], Spannable.SPAN_EXCLUSIVE_INCLUSIVE);//设置指定位置文字的颜色
        }
        return builder;
    }

    public static String str(EditText editText){
        return editText.getText().toString();
    }

    public static String str(Integer num){
        return num.toString();
    }

    public static String getThemState(Integer updateNum, Boolean finish) {
        if(finish){
            return String.format("全%d话",updateNum);
        }else {
            return String.format("更新至第%d话",updateNum);
        }
    }

    public static String getProgress(Integer progress) {
        if(progress==0){
            return "还没开始看";
        }else {
            return String.format("看完第%d话",progress);
        }
    }

    public static String getUpdateState(Boolean finish, Integer updateDay) {
        return String.format("%s,每周%s更新",finish?"已完结":"连载中",TimeUtils.getWeekStr(updateDay));
    }

    private static final String[] FOLLOW_STATES="想看,在看,看过".split(",");

    public static String getFollowState(FollowThem followThem) {
        if(followThem==null)return "追番";
        return FOLLOW_STATES[followThem.getState()];
    }

    public static String getFavoritesState(Integer count, Boolean opened) {
        return String.format("%d个内容·%s",count,opened?"公开":"私有");
    }

    public static SpannableStringBuilder formatDiscuss(List<Discuss> discusses) {
        if(discusses==null||discusses.isEmpty())return null;
        int num=Math.min(3,discusses.size());
        StringBuilder builder=new StringBuilder();
        int[] pos=new int[num*4];
        int index=0;
        for (int i = 0; i < num; i++) {
            Discuss discuss = discusses.get(i);
            Member member = discuss.getExtra().getMember();
            Member reply = discuss.getExtra().getReply();
            pos[index++]=builder.length();
            builder.append(member.getNickName());
            pos[index++]=builder.length();
            builder.append("\u0006回复\u0006");
            pos[index++]=builder.length();
            builder.append("@").append(reply.getNickName());
            pos[index++]=builder.length();
            builder.append(":\u0006").append(discuss.getComment()).append("\n");
        }
        return getColorText(builder.substring(0,builder.length()-1), 0xff2981aa,pos);
    }

    public static String str(Integer num, String def) {
        return num>0?num.toString():def;
    }

    private static final String[] TYPE_NAMES="video,play".split(",");

    public static String generateUrl(String baseUrl, Integer type, Integer targetId) {
        return baseUrl +TYPE_NAMES[type]+"/"+targetId;
    }

}